﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Net;
namespace LBSWD
{
    public partial class Form_WD : Form
    {
        public Form_WD()
        {
            InitializeComponent();
        }

        Queue m_qTI = new Queue();
        WebClient m_wc;

        private void timer_Tick_Tick(object sender, EventArgs e)
        {

            lock (m_qTI)
            {
                int nCount = m_qTI.Count;
                for (int i = 0; i < nCount; i++)
                {
                    TargetInstance ti = (TargetInstance)m_qTI.Dequeue();
                    string strTemp = ti.Check();
                    if (strTemp != "")
                    {
                        //Send to Logis

                        try
                        {
                            if (m_wc == null)
                            {
                                m_wc = new WebClient();
                            }

                          //  m_wc.OpenRead("http://log.logis.com.tw/WDLog.aspx?Data=" + strTemp);
                        }
                        catch (Exception ex)
                        {
                            Log(ex.Message);
                            Log(ex.StackTrace);
                            m_wc = null;
                        }

                        Log(strTemp);
                    }
                    m_qTI.Enqueue(ti);
                }
            }


        }




        private void Log(string arg_str)
        {
            TextWriter tw = new StreamWriter("VISWD" + System.DateTime.Now.ToString("yyyyMMdd") + ".log", true);
            tw.WriteLine(System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\t" + arg_str);
            tw.Close();

            if (this.listBox_Log.Items.Count > 500)
                this.listBox_Log.Items.RemoveAt(500);
            this.listBox_Log.Items.Insert(0, System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\t" + arg_str);

        }

        private void button_Start_Click(object sender, EventArgs e)
        {
            m_qTI.Enqueue(new TargetInstance("LBSDS.exe"));
            this.timer_Tick.Enabled = true;
        }

        private void button_Start2_Click(object sender, EventArgs e)
        {

        }

    }

    public class TargetInstance
    {
        int m_nSecs = 6;
        DateTime m_dtNextCheck = System.DateTime.Now;
        
        //string m_strLastLog;
        string m_strProcessName;

        public TargetInstance(string arg_strProcessName)
        {
            m_strProcessName = arg_strProcessName;
        }
        string strLastCheck = "";
        public string Check()
        {
            if (m_dtNextCheck < System.DateTime.Now)
            {
                m_dtNextCheck = System.DateTime.Now.AddSeconds(m_nSecs);
                System.Diagnostics.Process[] pAryOri = System.Diagnostics.Process.GetProcessesByName(m_strProcessName.Substring(0, m_strProcessName.Length - 4));
                if (pAryOri.Length != 0)
                {
                    Process p = pAryOri[0];
                    string strTemp =  m_strProcessName + ":W:" + (p.WorkingSet64 / 1000).ToString() + "K / P:" + (p.PagedMemorySize64 / 1000).ToString() + "K / PS:" + (p.PagedSystemMemorySize64 / 1000).ToString() + "K / V:" + (p.VirtualMemorySize64 / 1000).ToString() + "K / PW:" + (p.PeakWorkingSet64 / 1000).ToString() + "K / PP:" + (p.PeakPagedMemorySize64 / 1000).ToString() + "K / PV:" + (p.PeakVirtualMemorySize64 / 1000).ToString() + "K " + " @" + p.StartTime.ToString("yyyy/MM/dd HH:mm:ss");
                    if (strTemp != strLastCheck)
                    {
                        strLastCheck = strTemp;
                        return strTemp;
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            return "";
        }



    }
}